package logic;

import static view.Door.dirs;
import static view.Door.running;
import java.io.File;
import javax.swing.JScrollBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import entity.ButtonEnum;
import qw.easyFrame.throwables.DefinedRuntimeException;

/**
 * 按钮总控制器.
 * @author Rex
 *
 */
public class Controller {

	public static void action(JTextField input, JTextArea area, JScrollBar bar, ButtonEnum btn) {
		String msg = "程序正在执行，请勿重复点击";
		try {
			if (!running) {
				msg = "您未选择文件夹或文件夹无法访问";
				if (dirs != null && dirs.length != 0 && dirs[0].exists()) {
					running = true;
					msg = "请稍候……";
					new Thread(() -> {
						switch (btn) {
						case Re_Name:
							Rename.startup(input, area, bar);
							break;
						case Duplicate_Removal:
							DuplicateRemoval.startup(area, bar);
							break;
						case Files_Classify:
							FilesClassify.startup(area, bar);
							break;
						default:
							break;
						}
					}).start();
				}
			}
		} catch (Exception e) {
			msg = "程序出现未知异常！";
			new DefinedRuntimeException(msg, e);
		} finally {
			area.append(msg);
			// 拖动滚动条至最后一行
			bar.setValue(bar.getMaximum());
		}
	}
	
	/**
	 * 归档文件.
	 * @param target 目标文件夹
	 * @param file 文件
	 * @param fileName 新文件名
	 */
	static void classify(String target, File file, String fileName) {
		File newDir = new File(dirs[0] + File.separator + target);
		newDir.mkdir();
		file.renameTo(new File(newDir + File.separator + fileName));// 归档
	}
	
}